package com.atguigu.daijia.payment.listener;

import com.atguigu.daijia.common.constant.MqConst;
import com.atguigu.daijia.payment.service.WxPayService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @create: 2024/10/2
 * @author: gyhe10
 * @description: 监听支付成功消息队列，处理支付成功后的业务逻辑
 **/

@Slf4j
@Component
@RequiredArgsConstructor
public class PaymentListener {

    private final WxPayService wxPayService;

    /**
     * 监听支付成功消息队列，处理支付成功后的业务逻辑
     * @param orderNo
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_PAY_SUCCESS, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER),
            key = {MqConst.ROUTING_PAY_SUCCESS}
    ))
    public void paySuccess(String orderNo, Message message, Channel channel) throws IOException {
        wxPayService.handleOrder(orderNo);
        // 消费者手动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
